package com.pactera.madp.cp.service.sync.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pactera.madp.cp.api.entity.sync.MaHisDataBoardEntity;
import com.pactera.madp.cp.api.entity.sync.MaYesterdayDataBoardEntity;
import com.pactera.madp.cp.api.vo.employee.MarkingDataVo;
import com.pactera.madp.cp.api.vo.employee.SidebarMarkingDataTendencyVo;
import com.pactera.madp.cp.mapper.sync.MaHisDataBoardMapper;
import com.pactera.madp.cp.service.sync.MaHisDataBoardService;
import com.pactera.madp.cp.service.sync.MaYesterdayDataBoardService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @title 经理视图 -> 历史数据看板 业务接口实现
 * @date 2022-05-12
 * @author Deming.Chang
 */
@Service
@RequiredArgsConstructor
public class MaHisDataBoardServiceImpl extends ServiceImpl<MaHisDataBoardMapper, MaHisDataBoardEntity> implements MaHisDataBoardService {

    private final MaYesterdayDataBoardService maYesterdayDataBoardService;

    @Override
    public boolean insertMaDateData(String maDate) {
        return baseMapper.insertMaDateDataBoard(maDate);
    }

    @Override
    public SidebarMarkingDataTendencyVo hisData(String wxUserId, String date) {
        SidebarMarkingDataTendencyVo vo = new SidebarMarkingDataTendencyVo();
        MaHisDataBoardEntity entity = this.getOne(
                Wrappers.<MaHisDataBoardEntity>lambdaQuery()
                        .eq(MaHisDataBoardEntity::getDelFlag, 0)
                        .eq(MaHisDataBoardEntity::getRecommendId, wxUserId)
                        .eq(MaHisDataBoardEntity::getMaDate, date)
        );
        if (entity != null) {
            vo.setTime(date);
            vo.setVisitorNumber(entity.getTodayVisitors());
            vo.setBrowsesNumber(entity.getTodayBrowses());
        } else {
            vo.setTime(date);
            vo.setVisitorNumber(0L);
            vo.setBrowsesNumber(0L);
        }
        return vo;
    }

    @Override
    public MarkingDataVo marketingData(String wxUserId, Integer dateType) {
        String employeeId = wxUserId;
        MarkingDataVo vo = new MarkingDataVo();
        MaYesterdayDataBoardEntity entity = maYesterdayDataBoardService.getOne(
                Wrappers.<MaYesterdayDataBoardEntity>lambdaQuery()
                        .eq(MaYesterdayDataBoardEntity::getRecommendId, employeeId), false
        );
        Long visitorTotal = 0L;
        Long browsesTotal = 0L;
        List<SidebarMarkingDataTendencyVo> list = new ArrayList<>();
        if (dateType != null) {
            DateTime endTime = DateUtil.yesterday();
            DateTime end = DateUtil.beginOfDay(endTime);
            DateTime start = null;
            // dateType == 1，查近七日数据；否则查近3日数据
            if (dateType == 1) {
                if (entity != null) {
                    visitorTotal = entity.getYesterdaySevenDaysVisitors();
                    browsesTotal = entity.getYesterdaySevenDaysBrowses();
                }
                start = DateUtil.offsetDay(end, -7);
            } else {
                if (entity != null) {
                    visitorTotal = entity.getYesterdayThreeDaysVisitors();
                    browsesTotal = entity.getYesterdayThreeDaysBrowses();
                }
                start = DateUtil.offsetDay(end, -3);
            }
            List<MaHisDataBoardEntity> entities = this.list(
                    Wrappers.<MaHisDataBoardEntity>lambdaQuery()
                            .ge(MaHisDataBoardEntity::getMaDate, start)
                            .lt(MaHisDataBoardEntity::getMaDate, end)
                            .eq(MaHisDataBoardEntity::getRecommendId, employeeId)
            );
            DateTime now = DateUtil.offsetDay(start, 1);;
            outer:
            while (now.isBefore(endTime)) {
                String nowStr = DateUtil.formatDate(now);
                SidebarMarkingDataTendencyVo nvo = new SidebarMarkingDataTendencyVo();
                nvo.setTime(nowStr);
                nvo.setVisitorNumber(0L);
                nvo.setBrowsesNumber(0L);
                inner:
                for (MaHisDataBoardEntity entity1 : entities) {
                    if (nowStr.equals(entity1.getMaDate())) {
                        nvo.setVisitorNumber(entity1.getTodayVisitors());
                        nvo.setBrowsesNumber(entity1.getTodayBrowses());
                        now = DateUtil.beginOfDay(DateUtil.offsetDay(now, 1));
                        list.add(nvo);
                        continue outer;
                    }
                }
                list.add(nvo);
                now = DateUtil.beginOfDay(DateUtil.offsetDay(now, 1));
            }
        }
        vo.setVisitorTotal(visitorTotal);
        vo.setBrowsesTotal(browsesTotal);
        vo.setTendencies(list);
        return vo;
    }

}
